load("@llvm-project//mlir:tblgen.bzl", "gentbl_cc_library", "td_library")
load("@rules_cc//cc:defs.bzl", "cc_binary", "cc_library")

package(
    default_visibility = ["//visibility:public"],
    features = ["layering_check"],
    licenses = ["notice"],
)

exports_files(glob(["include/iree-dialects/Dialect/Input/*.td"]))

exports_files(glob(["python/*.cpp"]))

################################################################################
# Package-by-package filegroup exports.
# Some tools can make use of such non-nested groups.
################################################################################

filegroup(
    name = "python_dialects",
    srcs = glob(["python/iree/compiler/dialects/**/*.py"]),
)

################################################################################
# Tablegen exports
################################################################################

filegroup(
    name = "TdFilegroup",
    srcs = glob([
        "include/iree-dialects/Dialect/Input/*.td",
    ]),
)

td_library(
    name = "TdFiles",
    srcs = glob([
        "include/iree-dialects/Dialect/Input/*.td",
        "include/iree-dialects/Dialect/LinalgTransform/*.td",
        "python/iree/compiler/dialects/*.td",
    ]),
    includes = ["include"],
    deps = [
        "@llvm-project//mlir:BuiltinDialectTdFiles",
        "@llvm-project//mlir:InferTypeOpInterfaceTdFiles",
        "@llvm-project//mlir:OpBaseTdFiles",
        "@llvm-project//mlir:PDLDialectTdFiles",
        "@llvm-project//mlir:SideEffectInterfacesTdFiles",
        "@llvm-project//mlir:TransformDialectTdFiles",
    ],
)

################################################################################
# IREELinalgTransform Dialect
################################################################################

cc_library(
    name = "IREEDialectsTransforms",
    srcs = glob([
        "lib/Transforms/*.cpp",
    ]),
    hdrs = glob([
        "include/iree-dialects/Transforms/*.h",
    ]),
    includes = ["include"],
    deps = [
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:Analysis",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:DialectUtils",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FunctionInterfaces",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LinalgInterfaces",
        "@llvm-project//mlir:MathDialect",
        "@llvm-project//mlir:Rewrite",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TransformDialect",
        "@llvm-project//mlir:TransformDialectInterfaces",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:Transforms",
    ],
)

gentbl_cc_library(
    name = "IREELinalgTransformStructuredIncGen",
    strip_include_prefix = "include",
    tbl_outs = [
        (
            ["--gen-op-decls"],
            "include/iree-dialects/Dialect/LinalgTransform/StructuredTransformOpsExt.h.inc",
        ),
        (
            ["--gen-op-defs"],
            "include/iree-dialects/Dialect/LinalgTransform/StructuredTransformOpsExt.cpp.inc",
        ),
    ],
    tblgen = "@llvm-project//mlir:mlir-tblgen",
    td_file = "include/iree-dialects/Dialect/LinalgTransform/StructuredTransformOpsExt.td",
    deps = [
        ":TdFiles",
    ],
)

cc_library(
    name = "IREELinalgTransformDialect",
    srcs = glob([
        "lib/Dialect/LinalgTransform/IR/*.cpp",
        "lib/Dialect/LinalgTransform/IR/*.h",
    ]),
    hdrs = glob([
        "include/iree-dialects/Dialect/LinalgTransform/*.h",
    ]),
    includes = ["include"],
    deps = [
        ":IREEDialectsTransforms",
        ":IREELinalgTransformStructuredIncGen",
        "@llvm-project//llvm:Support",

        # Dialects
        "@llvm-project//mlir:AffineDialect",
        "@llvm-project//mlir:AffineUtils",
        "@llvm-project//mlir:AsyncDialect",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:BufferizationDialect",
        "@llvm-project//mlir:BufferizationTransforms",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FunctionInterfaces",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LLVMDialect",
        "@llvm-project//mlir:PDLDialect",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:SCFUtils",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TilingInterface",
        "@llvm-project//mlir:TransformDialect",
        "@llvm-project//mlir:TransformDialectInterfaces",
        "@llvm-project//mlir:TransformPDLExtension",

        # IR
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:Parser",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:Rewrite",

        # Interfaces
        "@llvm-project//mlir:ControlFlowInterfaces",

        # Transforms
        "@llvm-project//mlir:AffineToStandard",
        "@llvm-project//mlir:AsyncTransforms",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:MemRefTransforms",
        "@llvm-project//mlir:ReconcileUnrealizedCasts",
        "@llvm-project//mlir:SCFTransforms",
        "@llvm-project//mlir:TensorTransformOps",
        "@llvm-project//mlir:Transforms",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:VectorToSCF",

        # Utils
        "@llvm-project//mlir:ArithUtils",
        "@llvm-project//mlir:DialectUtils",

        # Conversions
        "@llvm-project//mlir:AsyncToLLVM",
        "@llvm-project//mlir:FuncToLLVM",
        "@llvm-project//mlir:IndexToLLVM",
        "@llvm-project//mlir:LinalgToStandard",
        "@llvm-project//mlir:MathToLLVM",
        "@llvm-project//mlir:MemRefToLLVM",
        "@llvm-project//mlir:SCFToControlFlow",
        "@llvm-project//mlir:VectorToLLVM",
    ],
)

cc_library(
    name = "IREELinalgTransformDialectPasses",
    srcs = glob([
        "lib/Dialect/LinalgTransform/Passes/*.cpp",
    ]),
    deps = [
        ":IREEDialectsTransforms",
        ":IREELinalgTransformDialect",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:AffineDialect",
        "@llvm-project//mlir:AffineUtils",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:ArithTransforms",
        "@llvm-project//mlir:BufferizationDialect",
        "@llvm-project//mlir:BufferizationTransforms",
        "@llvm-project//mlir:DialectUtils",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FunctionInterfaces",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:LLVMDialect",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:MathDialect",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:PDLDialect",
        "@llvm-project//mlir:PDLInterpDialect",
        "@llvm-project//mlir:Parser",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:Rewrite",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:SCFTransforms",
        "@llvm-project//mlir:SCFUtils",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TensorTransforms",
        "@llvm-project//mlir:TensorUtils",
        "@llvm-project//mlir:TransformDialect",
        "@llvm-project//mlir:TransformDialectInterfaces",
        "@llvm-project//mlir:TransformDialectTransforms",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:Transforms",
        "@llvm-project//mlir:VectorDialect",
        "@llvm-project//mlir:VectorToLLVM",
        "@llvm-project//mlir:VectorTransforms",
    ],
)

################################################################################
# CAPI
################################################################################

cc_library(
    name = "CAPI",
    srcs = glob(["lib/CAPI/*.cpp"]),
    hdrs = glob(["include/iree-dialects-c/*.h"]),
    includes = ["include"],
    deps = [
        ":IREELinalgTransformDialect",
        ":IREELinalgTransformDialectPasses",
        "@llvm-project//mlir:CAPIIR",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:LinalgTransformOps",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:TransformDialect",
    ],
)

################################################################################
# Test lib
################################################################################

cc_library(
    name = "IREEDialectsTest",
    srcs = glob([
        "test/lib/**/*.cpp",
    ]),
    deps = [
        ":IREEDialectsTransforms",
        ":IREELinalgTransformDialect",
        ":IREELinalgTransformDialectPasses",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:Pass",
        "@llvm-project//mlir:Rewrite",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:Transforms",
    ],
)

################################################################################
# Tools
################################################################################

cc_binary(
    name = "iree-dialects-opt",
    srcs = [
        "tools/iree-dialects-opt/iree-dialects-opt.cpp",
    ],
    tags = ["hostonly"],
    deps = [
        ":IREEDialectsTest",
        ":IREELinalgTransformDialect",
        ":IREELinalgTransformDialectPasses",
        "@llvm-project//llvm:Support",
        "@llvm-project//mlir:AffineDialect",
        "@llvm-project//mlir:ArithDialect",
        "@llvm-project//mlir:AsyncDialect",
        "@llvm-project//mlir:BufferizationTransformOps",
        "@llvm-project//mlir:ControlFlowDialect",
        "@llvm-project//mlir:FuncDialect",
        "@llvm-project//mlir:FuncExtensions",
        "@llvm-project//mlir:FunctionInterfaces",
        "@llvm-project//mlir:IR",
        "@llvm-project//mlir:LLVMDialect",
        "@llvm-project//mlir:LLVMIRTransforms",
        "@llvm-project//mlir:LinalgDialect",
        "@llvm-project//mlir:LinalgTransformOps",
        "@llvm-project//mlir:LinalgTransforms",
        "@llvm-project//mlir:MemRefDialect",
        "@llvm-project//mlir:MemRefTransforms",
        "@llvm-project//mlir:MlirOptLib",
        "@llvm-project//mlir:PDLDialect",
        "@llvm-project//mlir:PDLInterpDialect",
        "@llvm-project//mlir:SCFDialect",
        "@llvm-project//mlir:SCFTransformOps",
        "@llvm-project//mlir:SCFTransforms",
        "@llvm-project//mlir:Support",
        "@llvm-project//mlir:TensorDialect",
        "@llvm-project//mlir:TensorTransformOps",
        "@llvm-project//mlir:TransformDialect",
        "@llvm-project//mlir:TransformPDLExtension",
        "@llvm-project//mlir:TransformUtils",
        "@llvm-project//mlir:Transforms",
        "@llvm-project//mlir:VectorTransformOps",
    ],
)
